﻿using System.Diagnostics;
using System.IO;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Path = System.IO.Path;

namespace FileBrowserTool
{
    public class FileObj
    {
        public string Name { get; set; }

        public string Extension { get; set; }

        public long Size { get; set; }

        public string Path { get; set; }

    }

    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            LoadDirectories(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile));
        }

        private void LoadDirectories(string path)
        {
            DirectoryInfo dirInfo = new DirectoryInfo(path);

            TreeViewItem root = new TreeViewItem
            {
                Header = dirInfo.Name,
                Tag = dirInfo
            };

            root.Items.Add(null);
            root.Expanded += Folder_Expanded;
            root.MouseDoubleClick += Folder_Expanded;
            FoldersTree.Items.Add(root);
        }

        private void FilesList_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (FilesList.SelectedItem is FileObj selectedFile)
            {
                if (selectedFile.Extension == ".txt")
                {
                    Process.Start("notepad.exe", selectedFile.Path);
                }
            }
        }


        private void Folder_Expanded(object sender, RoutedEventArgs e)
        {
            TreeViewItem item = (TreeViewItem)sender;
            if (item.Items.Count == 1 && item.Items[0] == null)
            {
                item.Items.Clear();
                DirectoryInfo expandedDir = (DirectoryInfo)item.Tag;
                foreach (DirectoryInfo subDir in expandedDir.GetDirectories())
                {
                    TreeViewItem subItem = new TreeViewItem
                    {
                        Header = subDir.Name,
                        Tag = subDir
                    };
                    subItem.Items.Add(null);
                    subItem.Expanded += Folder_Expanded;
                    item.Items.Add(subItem);
                }
                foreach (FileInfo file in expandedDir.GetFiles())
                {
                    item.Items.Add(new TreeViewItem { Header = file.Name, Tag = file });
                }
                LoadFilesInListView(expandedDir.FullName);
            }
        }


        private void TreeView_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            TreeViewItem item = (TreeViewItem)FoldersTree.SelectedItem;
            if (item == null) return;
            if (item.Tag is DirectoryInfo dirInfo)
            {
                LoadFilesInListView(dirInfo.FullName);
            }
            else if (item.Tag is FileInfo fileInfo)
            {
                if (fileInfo.Extension.Equals(".txt"))
                {
                    Process.Start("notepad.exe", fileInfo.FullName);
                }
            }
        }


        private void Exit_Click(object sender, RoutedEventArgs e)
        {
            Application.Current.Shutdown();
        }

        private void Refresh_Click(object sender, RoutedEventArgs e)
        {
            FoldersTree.Items.Clear();
            LoadDirectories(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile));
            LoadFilesInListView(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile));
        }

        private void LoadFilesInListView(string path)
        {
            DirectoryInfo directoryInfo = new DirectoryInfo(path);
            FilesList.Items.Clear();
            foreach (FileInfo file in directoryInfo.GetFiles())
            {
                FilesList.Items.Add(new FileObj
                {
                    Name = file.Name,
                    Extension = file.Extension,
                    Size = file.Length,
                    Path = file.FullName
                });
            }

            foreach (DirectoryInfo dir in directoryInfo.GetDirectories())
            {
                FilesList.Items.Add(new FileObj
                {
                    Name = dir.Name,
                    Extension = "<DIR>",
                    Size = 0,
                    Path = dir.FullName
                });
            }
        }

        private void GoTo_Click(object sender, RoutedEventArgs e)
        {
            MenuItem menuItem = (MenuItem)sender;
            var folderName = menuItem.Header.ToString();
            string path = "";

            switch (folderName)
            {
                case "视频":
                    path = Environment.GetFolderPath(Environment.SpecialFolder.MyVideos);
                    break;
                case "图片":
                    path = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
                    break;
                case "桌面":
                    path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
                    break;
                case "家":
                    path = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
                    break;
            }

            LoadFilesInListView(path);
            FoldersTree.Items.Clear();
            LoadDirectories(path);
        }
    }
}